package tm.testing;

import com.jme.scene.Node;
import com.jme.scene.Spatial;


public class GridElement extends Node {
	
	// TODO Maybe add a method getCoordinate()
	//		getX() and getY() might cause bad handling (?)
	
	private static final long serialVersionUID = 1L;
	private int x;
	private int y;
	private Structure structure;
	private Stage stage;
	private Spatial trackpiece;
	
	public GridElement(int x, int y) {
		this.x = x;
		this.y = y;
		structure = null;
		stage = null;
		trackpiece = null;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public Coordinate getCoordinate() {
		return new Coordinate(x,y);
	}

	public void addStructure(Structure str) {
		structure = str;
		attachChild(structure.getSpatial());
		updateRenderState();
	}
	
	public void addTrackPiece(Spatial s) {
		trackpiece = s;
		attachChild(trackpiece);
		updateRenderState();
	}
	
	public void removeTrackPiece() {
		detachChild(trackpiece);
		trackpiece = null;
		updateRenderState();
	}
	
	public void addStage(Stage stg)
	throws NonInfrastructureGridElementException{
		stage = stg;
		attachChild(stage.getSpatial());
		updateRenderState();
	}
	
	public Spatial getTrackPiece() {
		return trackpiece;
	}
	
	public void removeStructure() {
		detachChild(structure.getSpatial());
		structure = null;
		updateRenderState();
	}
	
	public void removeStage() {
		detachChild(stage.getSpatial());
		stage = null;
		updateRenderState();
	}
	
	public boolean isEmpty() {
		if(structure == null && trackpiece == null && stage == null) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean hasStructure() {
		if(structure!=null) {
			return true; 
		} else {
			return false;
		}
	}
	
	public boolean hasTrackPiece() {
		if(trackpiece!=null) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean hasStage() {
		if(stage!=null) {
			return true; 
		} else {
			return false;
		}
	}
	
	/**
	public boolean isInfrastructure() {
		return null;
	}
	*/
	
	public String toString() {
		return "GridElement(x="+x+"|y="+y+")";
	}
}
